- @pagetitle = 'Your Canned Responses'

.card.mb-2
  .card-body#canned-responses
    %h4 Your canned responses
    - if @canned_responses.present?
      .d-flex.flex-wrap.gap-4.align-self-stretch.pt-4
        - @canned_responses.each do |canned_response|
          = render(CardComponent.new) do |component|
            - if canned_response.decision_type
              %p.card-text
                %b Decision's reason:
                %span.mb-2= canned_response.decision_type.humanize
            %p.card-text
              %b Content:
              .canned-response-card-text.mb-2
                = render partial: 'webui/shared/collapsible_text', locals: { text: canned_response.content, render_markdown: true }

            - component.with_header do
              = canned_response.title

            - component.with_delete_button do
              = link_to '#', title: 'Delete Canned Response',
                data: { 'bs-toggle': 'modal', 'bs-target': '#delete-canned-response-modal',
                        canned_response_title: canned_response.title, action: canned_response_path(canned_response) } do
                %i.fas.fa-times-circle.text-danger

            - component.with_action do
              = link_to(edit_canned_response_path(canned_response), title: 'Edit Canned Response', class: 'nav-link p-1') do
                %i.fas.fa-edit
                Edit

      .modal.fade#delete-canned-response-modal{ tabindex: -1, role: 'dialog',
                                                aria: { labelledby: 'delete-canned-response-modal-label', hidden: true } }
        .modal-dialog.modal-dialog-centered{ role: 'document' }
          .modal-content
            .modal-header
              %h5.modal-title#delete-canned-response-modal-label Do you really want to delete this canned response?
            .modal-body
              %p
                Please confirm that you want to delete the canned response with title
                = surround "'" do
                  %span#canned-response-title
              = form_tag nil, method: :delete do
                .modal-footer
                  %a.btn.btn-sm.btn-outline-secondary.px-4{ data: { 'bs-dismiss': 'modal' } }
                    Cancel
                  %input.btn.btn-sm.btn-danger.px-4{ type: 'submit', name: 'commit', value: 'Delete', data: { disable: { with: 'Delete' } } }

      - content_for :ready_function do
        :plain
          $('#delete-canned-response-modal').on('show.bs.modal', function (event) {
            var link = $(event.relatedTarget);
            $(this).find('#canned-response-title').text(link.data('canned-response-title'));
            $(this).find('form').attr('action', link.data('action'));
          })

    - else
      %p.m-4 No canned responses yet

    .pt-4
      = link_to '#', title: 'Create Canned Response', data: { 'bs-toggle': 'modal', 'bs-target': '#create-canned-response-modal' } do
        %i.fas.fa-plus-circle.text-primary
        Create Canned Response

.modal.fade#create-canned-response-modal{ tabindex: -1, role: 'dialog', aria: { labelledby: 'create-canned-response-modal-label', hidden: true } }
  .modal-dialog.modal-dialog-centered{ role: 'document' }
    .modal-content
      .card
        .card-body
          %h4 Create a new canned response
          = form_for(@canned_response) do |f|
            .mb-3
              = f.label :title, 'Title:'
              = f.text_field :title, class: classes_with_validation(@canned_response, :title), required: true, maxlength: 255
            .mb-3
              = f.label :content, 'Content:'
              = f.text_area :content, rows: 5, class: classes_with_validation(@canned_response, :content), required: true, maxlength: 65535
            - if policy(Decision.new).create?
              .mb-3
                = f.label :decision_type, "Choose 'cleared' or 'favored' if this is a decision's reason"
                - types = CannedResponse.decision_types.keys.to_h { |t| [t.humanize, t] }
                = f.select(:decision_type, types, { include_blank: 'none', selected: nil }, class: 'form-select')
            = f.submit 'Create', class: 'btn btn-primary'
            = button_tag('Cancel', type: 'reset', class: 'btn btn-light',
                         data: { 'bs-toggle': 'modal', 'bs-target': '#create-canned-response-modal' })
